<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\admin\controller\api;
use app\common\controller\Api;
/**
 * 流程明细管理
 * @package app\enterprise
 */
class Workstep extends Api

{

    /**
   * 流程明细列表
   * @param  流程ID
   * @return  json 
   * 
   *
   */

	public function workstepList($wf){
            $model=model('common/workstep');
            if (empty($wf)) {
              return json(['code'=>0,'msg'=>'流程不存在,请检查']);
            }
             $where['wfFlowInfoId'] = $wf ;
              if(($limit = input('limit')) && ($page = input('page'))){
               $startLimit =  ($page-1)*$limit;

               $info =model('common/workstep')->where($where)->order('orderIndex')->limit($startLimit.','.$limit)->select();
             }else{
               $info =collection(model('common/workstep')->where($where)->select())->toArray();
             }
      
               $count = count($info); 
               
               return json(['code'=>1,'msg'=>'流程信息','count'=>$count,'data'=>$info]);
 	 }
   	  
   	      //添加流程
   	 public function add($data){
           if (empty($data)) {
               return json(['code'=>0,'msg'=>'数据异常,请检查']);
           }
           if (empty($data['wfFlowInfoId'])) {
               return json(['code'=>0,'msg'=>'流程不存在,请检查']);
           }
           $model = model('common/workstep');
           if(false == $model->save($data)){
           		 return json(['code'=>0,'msg'=>'添加失败,请检查']);
           }else{
           	//自增步骤数
           		model('Workflow')->where('id', $data['wfFlowInfoId'])->setInc('stepTotal');
               setlog('workstepadd','workstep',session('user_auth.id'));
           		return json(['code'=>1,'msg'=>'流程步骤添加成功']);
           }
   	 }
   	
   /**
   * 修改流程
   * @param string workstepId 流程id,
   * @return json
   *
   */
   	public function edit($data = null,$workstepId = null){
          if(empty($workstepId)){
              return json(['code'=>0,'msg'=>'流程步骤不存在,请检查']);
          }
        	$model = model('common/workstep');
          if ($model->change($data)) {
             setlog('workstepupdate','workstep',session('user_auth.id'));
	             return json(['code'=>1,'msg'=>'用户修改成功']);
	            }else{
	             return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
	            }
	       
    	  return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
   }

   	 
     
    /**
   *  删除流程
   * @param  string workstepId 用户id,
   * @return  json
   *
   */
     
    public function del($workstepId) {
        $model =  model('common/workstep');
        if(!$workstepId){
        	return  json(['code'=>0,'msg'=>'流程ID不存在,请检查']);
        }
		if (!$step = $model->find($workstepId)) {
          	return  json(['code'=>0,'msg'=>'流程步骤不存在,请检查']);
        }
        if($model->destroy(['id'=>$workstepId]) !== FALSE){
        	//自减步骤数
        	model('Workflow')->where('id', $step['wfFlowInfoId'])->setDec('stepTotal');
           setlog('workstepdel','workstep',session('user_auth.id'));
        	return json(['code'=>1,'msg'=>'流程删除成功']);
        }else{
         	return $this->json(['code'=>0,'msg'=>'流程删除失败,请检查']);
        }
    }


    /**
   *  用户列表
   * @param  
   * @return  json
   *
   */
     
    public function getUser() {
            $user = model('common/User');
              //角色列表
            $users = $user->field('id,userName')->order(array('id' => 'DESC'))->select();
       		return json(['code'=>1,'msg'=>'用户列表','data'=>$users]);
    }

    /**
   * 流程列表
   * @param  
   * @return  json
   *
   */
     
    public function getWf() {
            $workflow=model('common/Workflow');
              //角色列表
            $workflows =$workflow->field('id,flowName')->order(array('id' => 'DESC'))->select();
        return json(['code'=>1,'msg'=>'流程列表','data'=>$workflows]);
    }


}